<template>
  <div class="index-lists">
    <el-card class="!border-none" shadow="never">
      <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
        <el-form-item label="图书id" prop="id">
          <el-input class="w-[280px]" v-model="queryParams.id" />
        </el-form-item>
        <el-form-item label="书名" prop="name">
          <el-input class="w-[280px]" v-model="queryParams.name" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none mt-4" shadow="never">
      <div>
        <el-button v-perms="['wisdbook:add']" type="primary" @click="handleAdd()">
          <template #icon>
            <icon name="el-icon-Plus" />
          </template>
          新增
        </el-button>
      </div>
      <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists">
        <el-table-column label="图书id" prop="id" min-width="100" />
        <el-table-column label="书名" prop="name" min-width="100" />
        <el-table-column label="年级" prop="grade" min-width="100">
          <template #default="{ row }">
            <dict-value :options="dictData.年级" :value="row.grade" />
          </template>
        </el-table-column>
        <el-table-column label="科目" prop="subject" min-width="100">
          <template #default="{ row }">
            <dict-value :options="dictData.科目" :value="row.subject" />
          </template>
        </el-table-column>
        <el-table-column label="学段" prop="section" min-width="100">
          <template #default="{ row }">
            <dict-value :options="dictData.学段" :value="row.section" />
          </template>
        </el-table-column>
        <el-table-column label="教材版本" prop="edition" min-width="100">
          <template #default="{ row }">
            <dict-value :options="dictData.版本" :value="row.edition" />
          </template>
        </el-table-column>
        <el-table-column label="分册" prop="volume" min-width="100">
          <template #default="{ row }">
            <dict-value :options="dictData.分册" :value="row.volume" />
          </template>
        </el-table-column>
        

        <el-table-column label="标注员" prop="markUserName" min-width="100" />

        <el-table-column label="父书id" prop="pid" min-width="100" />

        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">

            <el-button v-perms="['wisdbook:order']" type="primary" link @click="handleOrder(row)">
              指派
            </el-button>

            <el-button v-perms="['wisdbook:edit']" type="primary" link @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button v-perms="['wisdbook:del']" type="danger" link @click="handleDelete(row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />



    <el-dialog v-model="markerOrderDialogVisible" title="标注指派" width="40%">
      <div class="">
        <div>书名：{{ choseRow.name }}</div>
        <div v-if="lastOrderUser.name">此书已经指派给：{{ lastOrderUser.name }}</div>
        <div>
          <span>指派给：</span>
          <el-select v-model="orderUser" class="m-2 ml-2" placeholder="选择指派人" @change="handeChangeOrderUser()">
            <el-option v-for="item in users" :key="item.id" :label="item.nickname" :value="item.id" />
          </el-select>
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="markerOrderDialogVisible = false">取消</el-button>
          <el-button v-if="users.length>0" type="primary" @click="handeSureOrderUser()">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>

  </div>
</template>
<script lang="ts" setup name="wisdbook">
import { wisdbookDelete, wisdbookLists, userAdd,
   userEdit, getMarkerUserWithBookId, 
   orderUserSelect } from "@/api/wisdbook";
import { useDictData } from "@/hooks/useDictOptions";
import { usePaging } from "@/hooks/usePaging";
import feedback from "@/utils/feedback";
import EditPopup from "./edit.vue";

const editRef = shallowRef<InstanceType<typeof EditPopup>>();
const showEdit = ref(false);
const queryParams = reactive({
  id: "",
  name: "",
});

const { pager, getLists, resetPage, resetParams } = usePaging({
  fetchFun: wisdbookLists,
  params: queryParams,
});

const { dictData } = useDictData<{
  年级: any[];
  科目: any[];
  学段: any[];
  版本: any[];
  分册: any[];
  markStatus: any[];
  addStacks: any[];
}>(["年级", "科目", "学段", "版本", "分册", "markStatus", "addStacks"]);

const handleAdd = async () => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("add");
};

const handleEdit = async (data: any) => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("edit");
  editRef.value?.getDetail(data);
};

//
let choseRow = ref({});
let users = ref([]);
let orderUser = ref({})
let lastOrderUser = ref({})

let markerOrderDialogVisible = ref(false);
const handleOrder = async (data: any) => {
  choseRow.value = data;
  lastOrderUser.value = {}
  users.value=[]
  await nextTick();
  markerOrderDialogVisible.value = true
  let bookUser = await getMarkerUserWithBookId({ bookId: data.id })
  let ret = await orderUserSelect({ pageNo: 1, pageSize: 50,ids:'1,2'})
  if (bookUser) {
    let tmp = ret.lists.filter(item => item.id == bookUser.markUserId)
    ret.lists = ret.lists.filter(item => item.id != bookUser.markUserId)
    if(tmp&&tmp[0]&&tmp[0].nickname){
      bookUser.name = tmp[0].nickname
    }
    lastOrderUser.value = bookUser
  }
  users.value = ret.lists;
};

const handeChangeOrderUser = () => {
  console.log('handeChangeOrderUser', orderUser.value);
}

const handeSureOrderUser = async () => {

  markerOrderDialogVisible.value = false
  if (!lastOrderUser.value) {
    lastOrderUser.value = {
    }
  }
  lastOrderUser.value.bookId = choseRow.value.id
  if (typeof orderUser.value == 'number'){
    lastOrderUser.value.markUserId = orderUser.value
  }else{
    lastOrderUser.value.markUserId = users.value[0].id
  }
  console.log('handeSureOrderUser', lastOrderUser.value);
  let upload = lastOrderUser.value
  
  if (upload.id) {//update
     await userEdit({ ...upload })
  } else {//add
     await userAdd({ ...upload })
  }
  feedback.msgSuccess('指派成功')


  
}
//

const handleDelete = async (id: number) => {
  await feedback.confirm("确定要删除？");
  await wisdbookDelete({ id });
  feedback.msgSuccess("删除成功");
  getLists();
};

getLists();
</script>
